

#include "Helpers4XML.h"

#include "tinyxml2.h"
#include <iostream>

using std::cout;

#ifdef USING_TINYXML2


vector<string>& SplitString2Vector(vector<string>& result, const char* constStr, const char* delimiters)
{
  result.clear();
  char *str = new char[strlen(constStr) + 1];
  std::strcpy ( str, constStr );
  char* pch = strtok(str, delimiters);
  while (delimiters != NULL )
  {
    result.push_back(pch);
    pch = strtok (NULL, delimiters);
  }
  delete str;
  delete pch;
  return result;
}

vector<float>& SplitString2Vector(vector<float>& result, const char* constStr, const char* delimiters)
{
  result.clear();
  char *str = new char[strlen(constStr) + 1];
  std::strcpy ( str, constStr );
  char* pch = strtok(str, delimiters);
  while (pch != NULL )
  {
    result.push_back(atof(pch));
    pch = strtok (NULL, delimiters);
  }
  delete str;
  delete pch;
  return result;
}


vector<double>& SplitString2Vector(vector<double>& result, const char* constStr, const char* delimiters)
{
  result.clear();
  char *str = new char[strlen(constStr) + 1];
  std::strcpy ( str, constStr );
  cout << "str: " << str << "\n";
  char* pch = strtok(str, delimiters);
  while (pch != NULL )
  {
    result.push_back(strtod (pch,NULL));
    pch = strtok (NULL, delimiters);
  }
  delete str;
  delete pch;
  return result;
}

vector<int>& SplitString2Vector(vector<int>& result, const char* constStr, const char* delimiters)
{
  result.clear();
  char *str = new char[strlen(constStr) + 1];
  std::strcpy ( str, constStr );
  char* pch = strtok(str, delimiters);
  while (pch != NULL )
  {
    result.push_back(atoi(pch));
    pch = strtok (NULL, delimiters);
  }
  delete str;
  delete pch;
  return result;
}

vector<string>& SplitString2Vector(vector<string>& result, const string& constStr, const char* delimiters)
{
  result.clear();
  char *str = new char[constStr.size() + 1];
  std::strcpy ( str, constStr.c_str() );
  char* pch = strtok(str, delimiters);
  while (delimiters != NULL )
  {
    result.push_back(pch);
    pch = strtok (NULL, delimiters);
  }
  delete str;
  delete pch;
  return result;
}

vector<float>& SplitString2Vector(vector<float>& result, const string& constStr, const char* delimiters)
{
  result.clear();
  char *str = new char[constStr.size() + 1];
  std::strcpy ( str, constStr.c_str() );
  char* pch = strtok(str, delimiters);
  while (pch != NULL )
  {
    result.push_back(atof(pch));
    pch = strtok (NULL, delimiters);
  }
  delete str;
  delete pch;
  return result;
}


vector<double>& SplitString2Vector(vector<double>& result, const string& constStr, const char* delimiters)
{
  result.clear();
  char *str = new char[constStr.size() + 1];
  std::strcpy ( str, constStr.c_str() );
  char* pch = strtok(str, delimiters);
  while (pch != NULL )
  {
    result.push_back(strtod (pch,NULL));
    pch = strtok (NULL, delimiters);
  }
  delete str;
  delete pch;
  return result;
}

vector<int>& SplitString2Vector(vector<int>& result, const string& constStr, const char* delimiters)
{
  result.clear();
  char *str = new char[constStr.size() + 1];
  std::strcpy ( str, constStr.c_str() );
  char* pch = strtok(str, delimiters);
  while (pch != NULL )
  {
    result.push_back(atoi(pch));
    pch = strtok (NULL, delimiters);
  }
  delete str;
  delete pch;
  return result;
}


string& Vector2String(string& result, const vector<string>& source, const char* jointer, const vector<string>& prefix)
{
  //const char* prefix = ""; 
  result = "";
  if ( 0 == source.size())
  {
    return result;
  }
  char tmp[BUF_SIZE];
  if ( 0 == prefix.size())
  {
    for (int i=0;i<source.size()-1;i++)
    {
      sprintf(tmp, "%s%s",source[i],jointer);
      result.append(tmp);
    }
    sprintf(tmp, "%s",source[source.size()-1]);
    result.append(tmp);
    return result;
  }
  else if( prefix.size() != source.size())
  {
    for (int i=0;i<source.size()-1;i++)
    {
      sprintf(tmp, "%s%s%s",prefix[0],source[i],jointer);
      result.append(tmp);
    }
    sprintf(tmp, "%s%s",prefix[0],source[source.size()-1]);
    result.append(tmp);
    return result;
  }
  else
  {
    for (int i=0;i<source.size()-1;i++)
    {
      sprintf(tmp, "%s%s%s",prefix[i],source[i],jointer);
      result.append(tmp);
    }
    sprintf(tmp, "%s%s",prefix[source.size()-1],source[source.size()-1]);
    result.append(tmp);
    return result;
  }
}
string& Vector2String(string& result, const vector<double>& source, const char* jointer, const vector<string>& prefix)
{
  result = "";
  if ( 0 == source.size())
  {
    return result;
  }
  char tmp[BUF_SIZE];
  if ( 0 == prefix.size())
  {
    for (int i=0;i<source.size()-1;i++)
    {
      sprintf(tmp, "%f%s",source[i],jointer);
      result.append(tmp);
    }
    sprintf(tmp, "%f",source[source.size()-1]);
    result.append(tmp);
    return result;
  }
  else if( prefix.size() != source.size())
  {
    for (int i=0;i<source.size()-1;i++)
    {
      sprintf(tmp, "%s%f%s",prefix[0],source[i],jointer);
      result.append(tmp);
    }
    sprintf(tmp, "%s%f",prefix[0],source[source.size()-1]);
    result.append(tmp);
    return result;
  }
  else
  {
    for (int i=0;i<source.size()-1;i++)
    {
      sprintf(tmp, "%s%f%s",prefix[i],source[i],jointer);
      result.append(tmp);
    }
    sprintf(tmp, "%s%f",prefix[source.size()-1],source[source.size()-1]);
    result.append(tmp);
    return result;
  }
}
string& Vector2String(string& result, const vector<float>& source, const char* jointer, const vector<string>& prefix)
{
  result = "";
  if ( 0 == source.size())
  {
    return result;
  }
  char tmp[BUF_SIZE];
  if ( 0 == prefix.size())
  {
    for (int i=0;i<source.size()-1;i++)
    {
      sprintf(tmp, "%f%s",source[i],jointer);
      result.append(tmp);
    }
    sprintf(tmp, "%f",source[source.size()-1]);
    result.append(tmp);
    return result;
  }
  else if( prefix.size() != source.size())
  {
    for (int i=0;i<source.size()-1;i++)
    {
      sprintf(tmp, "%s%f%s",prefix[0],source[i],jointer);
      result.append(tmp);
    }
    sprintf(tmp, "%s%f",prefix[0],source[source.size()-1]);
    result.append(tmp);
    return result;
  }
  else
  {
    for (int i=0;i<source.size()-1;i++)
    {
      sprintf(tmp, "%s%f%s",prefix[i],source[i],jointer);
      result.append(tmp);
    }
    sprintf(tmp, "%s%f",prefix[source.size()-1],source[source.size()-1]);
    result.append(tmp);
    return result;
  }
}
string& Vector2String(string& result, const vector<int>& source, const char* jointer, const vector<string>& prefix)
{

  result = "";
  if ( 0 == source.size())
  {
    return result;
  }
  char tmp[BUF_SIZE];
  if ( 0 == prefix.size())
  {
    for (int i=0;i<source.size()-1;i++)
    {
      sprintf(tmp, "%d%s",source[i],jointer);
      result.append(tmp);
    }
    sprintf(tmp, "%d",source[source.size()-1]);
    result.append(tmp);
    return result;
  }
  else if( prefix.size() != source.size())
  {
    for (int i=0;i<source.size()-1;i++)
    {
      sprintf(tmp, "%s%d%s",prefix[0],source[i],jointer);
      result.append(tmp);
    }
    sprintf(tmp, "%s%d",prefix[0],source[source.size()-1]);
    result.append(tmp);
    return result;
  }
  else
  {
    for (int i=0;i<source.size()-1;i++)
    {
      sprintf(tmp, "%s%d%s",prefix[i],source[i],jointer);
      result.append(tmp);
    }
    sprintf(tmp, "%s%d",prefix[source.size()-1],source[source.size()-1]);
    result.append(tmp);
    return result;
  }
}


void helpTest()
{

  vector<float> vec;
  vec.push_back(0.0);
  vec.push_back(2.5);
  vec.push_back(5.0);
  string vecstr;
  Vector2String(vecstr, vec, "a-  -  -");
  cout << vecstr << "\n";
  vector<float> int_vec;
  SplitString2Vector(int_vec, vecstr, "a -");
  cout << int_vec.size() << "\n";
  cout << Vector2String(string(""), int_vec, " = ") << "\n";

  char str[] ="- 111,  - - - 222,  - - -   333.";
  char * pch;
  printf ("Splitting string \"%s\"into tokens:\n",str);
  pch = strtok (str,",.- ");
  vector<int> vec2;
  while (pch != NULL)
  {
    vec2.push_back(atoi(pch));
    printf ("%i\n",vec2.back());
    pch = strtok (NULL, ",. -");
  }
  cout << "-------------\n";
  char str2[] ="- 111,  - - - 222,  - - -   333.";
  SplitString2Vector(vec2, str2, ",.- ");
  cout << vec2.size() << "\n";
  cout << Vector2String(string(""), vec2, " = ") << "\n";
}

#endif